package com.sky.zzh.devicemanager;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;

import com.next.intf.ITaskListener;
import com.next.net.SHGet;
import com.next.net.SHPost;
import com.next.net.SHPut;
import com.next.net.SHTask;
import com.sky.widget.SHDialog;
import com.sky.widget.SHDialog.DialogItemClickListener;
import com.sky.widget.SHToast;
import com.sky.widget.sweetdialog.SweetDialog;
import com.sky.zzh.BaseFragment;
import com.sky.zzh.R;
import com.sky.zzh.SHContainerActivity;
import com.sky.zzh.adapter.DeviceAdapter;
import com.sky.zzh.today.date.SliderContainer;
import com.sky.zzh.util.ConfigDefinition;
import com.sky.zzh.util.UserInfoManager;
import com.sky.zzh.view.SlidingMenu;
/**
 * 设备管理
 * @author skypan
 *
 */
public class DeviceManagerFragment extends BaseFragment implements ITaskListener{

	private ImageView mBtnAdd;
	private ListView mLvDevice;
	private SHGet listTask;
	private SHPut deleteTask,reActiveTask;
	private SHPost reNameTask;
	private JSONArray deviceArray;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.fragment_device_manager, container, false);
		return view;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onViewCreated(view, savedInstanceState);
		mBtnAdd = (ImageView) view.findViewById(R.id.btn_right);
		mBtnAdd.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getActivity(),SHContainerActivity.class);
				intent.putExtra("class", AddNameFragment.class.getName());
				startActivity(intent);
			}
		});
		mLvDevice = (ListView) view.findViewById(R.id.lv_device);
//		mLvDevice.setOnItemLongClickListener(new OnItemLongClickListener() {
//
//			@Override
//			public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
//				// TODO Auto-generated method stub
//				System.out.println("long click..");
//				final String[] items = new String[]{"删除","重命名","重激活"};
//				SHDialog.showActionSheet(getActivity(), "选择操作", items, new DialogItemClickListener() {
//					
//					@Override
//					public void onSelected(String result) {
//						// TODO Auto-generated method stub
//						if(result.equals(items[0])){
//							
//						}else if(result.equals(items[1])){
//							
//						}else if(result.equals(items[2])){
//							
//						}
//					}
//				});
//				return false;
//			}
//		});
		
		mLvDevice.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, final int pos, long arg3) {
				// TODO Auto-generated method stub
				if(!SlidingMenu.isOpen){
					final String[] items = new String[]{"删除","重命名","重激活","切换设备"};
					SHDialog.showActionSheet(getActivity(), "选择操作", items, new DialogItemClickListener() {
						
						@Override
						public void onSelected(String result) {
							// TODO Auto-generated method stub
							if(result.equals(items[0])){
								if("True".equals(deviceArray.optJSONObject(pos).optString("IsActivated"))){
									new SweetDialog(getActivity(), SweetDialog.NORMAL_TYPE).setTitleText("提示").setContentText("不能删除当前设备").show(); 
								}else{
									SHDialog.ShowProgressDiaolg(getActivity());
									deleteTask = new SHPut();
									deleteTask.setUrl(ConfigDefinition.URL+"user/DeleteUserDevice");
									deleteTask.setListener(DeviceManagerFragment.this);
									deleteTask.getTaskArgs().put("UserID", UserInfoManager.getInstance().getUserId());
									deleteTask.getTaskArgs().put("UUID", deviceArray.optJSONObject(pos).optString("UUID"));
									deleteTask.start();
								}
							}else if(result.equals(items[1])){
								Intent intent = new Intent(getActivity(),SHContainerActivity.class);
								intent.putExtra("class", RenameFragment.class.getName());
								intent.putExtra("name", deviceArray.optJSONObject(pos).optString("Description"));
								intent.putExtra("UUID", deviceArray.optJSONObject(pos).optString("UUID"));
								startActivity(intent);
							}else if(result.equals(items[2])){
//								SHDialog.ShowProgressDiaolg(getActivity());
//								reActiveTask = new SHPost();
//								reActiveTask.setUrl(ConfigDefinition.URL+"user/ActivateUserDevice");
//								reActiveTask.setListener(DeviceManagerFragment.this);
//								reActiveTask.getTaskArgs().put("UserID", UserInfoManager.getInstance().getUserId());
//								reActiveTask.getTaskArgs().put("UUID", deviceArray.optJSONObject(pos).optString("UUID"));
//								reActiveTask.start();
								Intent intent = new Intent(getActivity(),SHContainerActivity.class);
								intent.putExtra("class", ActiveDeviceFragment.class.getName());
								intent.putExtra("UUID", deviceArray.optJSONObject(pos).optString("UUID"));
								startActivity(intent);
							}else if(result.equals(items[3])){
								//切换设备
								SHDialog.ShowProgressDiaolg(getActivity());
								reActiveTask = new SHPut();
								reActiveTask.setUrl(ConfigDefinition.URL+"user/ActivateUserDevice");
								reActiveTask.setListener(DeviceManagerFragment.this);
								reActiveTask.getTaskArgs().put("UserID", UserInfoManager.getInstance().getUserId());
								reActiveTask.getTaskArgs().put("UUID", deviceArray.optJSONObject(pos).optString("UUID"));
								reActiveTask.start();
								UserInfoManager.getInstance().setUUID(deviceArray.optJSONObject(pos).optString("UUID"));
								UserInfoManager.getInstance().sync(getActivity(), true);
							}
						}
					});
				}
			}
		});
	}

	private void requestList(){
		SHDialog.ShowProgressDiaolg(getActivity());
		listTask = new SHGet();
		listTask.setListener(DeviceManagerFragment.this);
		listTask.setUrl(ConfigDefinition.URL+"user/UserDeviceList");
		listTask.getTaskArgs().put("UserID", UserInfoManager.getInstance().getUserId());
		listTask.start();
	}
	
	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		requestList();
	}

	@Override
	public void onTaskFinished(SHTask task) throws Exception {
		// TODO Auto-generated method stub
		SHDialog.dismissProgressDiaolg();
		System.out.println(task.getResult());
		JSONObject json = new JSONObject(task.getResult().toString());
		if("200".equals(json.optString("ReturnCode"))){
			if(task == listTask){
				deviceArray = json.optJSONArray("DeviceList");
				DeviceAdapter adapter = new DeviceAdapter(getActivity());
				adapter.setJsonArray(deviceArray);
				mLvDevice.setAdapter(adapter);
			}else if(task == deleteTask){
				requestList();
			}else if(task == reActiveTask){
				SHToast.showToast(getActivity(), "切换成功");
				Intent intent = new Intent();
				intent.setAction("device_change");
				getActivity().sendBroadcast(intent);
				requestList();
			}
		}else{
			new SweetDialog(getActivity(), SweetDialog.ERROR_TYPE).setTitleText("提示").setContentText(json.optString("ErrorMessage")).show();
		}
	}

	@Override
	public void onTaskFailed(SHTask task) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onTaskUpdateProgress(SHTask task, int count, int total) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onTaskTry(SHTask task) {
		// TODO Auto-generated method stub
		
	}

}
